package com.ws.hzuoj.admin.user.user.web;

import cn.hutool.core.util.StrUtil;
import com.ws.common.easyui.util.Page;
import com.ws.common.easyui.util.PageUtils;
import com.ws.user.user.entity.User;
import com.ws.user.user.entity.UserRole;
import com.ws.user.user.service.IUserRoleService;
import com.ws.user.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

@RequestMapping("/admin/user/user")
@Controller("adminUser")
public class UserWeb {


    @Autowired
    IUserService userService;
    @Autowired
    IUserRoleService userRoleService;

    @RequestMapping("{type}")
    public String list(@PathVariable String type){
        return  null;
    }

    @RequestMapping("/userRoleSave")
    @ResponseBody
    public Object userRoleSave(String userId,String[] roleIds){
        UserRole userRole=new UserRole();
        userRole.setUserId(userId);
        userRoleService.deleteByUserId(userId);
        for(String roleId :roleIds){
            userRole.setRoleId(roleId);
            userRole.setId(null);
            userRoleService.insert(userRole);
        }
        return "";
    }

    @RequestMapping("/usersRoleSave")
    @ResponseBody
    public Object usersRoleSave(String[] userIds,String roleId){
        UserRole userRole=new UserRole();
        userRole.setRoleId(roleId);
        for(String userId :userIds){
            userRole.setId(null);
            userRole.setUserId(userId);
            Boolean isExist=userRoleService.isExist(userRole);
            if(!isExist){
                userRoleService.insert(userRole);
            }

        }
        return "";
    }

    @RequestMapping("/deleteUserRole")
    @ResponseBody
    public Object deleteUserRole(String userRoleId){
        userRoleService.delete(userRoleId);
        return "";
    }


    @RequestMapping("/listData")
    @ResponseBody
    public Object listData(User user,Page page){
        List<User> users=userService.list(user,page);
        return PageUtils.getEasyUiList(users);
    }

    @RequestMapping("/listByRoleId")
    @ResponseBody
    public Object listByRoleId(String roleId, Page page){
        List<User> users=userService.listByRoleId(roleId,page);
        return PageUtils.getEasyUiList(users);
    }

    @RequestMapping("read")
    @ResponseBody
    public Object read(User user){
        if(StrUtil.isNotEmpty(user.getId())){
            user= userService.read(user.getId());
        }
        return  user;
    }

    @RequestMapping("/save")
    @ResponseBody
    public Object save(User user){
        Integer count=userService.save(user);
        return  count;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Integer delete(String id){
        return  userService.delete(id);
    }

    @RequestMapping("/insert")
    @ResponseBody
    public Object insert(User user){
        Integer count=userService.insert(user);
        return  count;
    }

    @RequestMapping("/insertSelective")
    @ResponseBody
    public Object insertSelective(User user){
        return userService.insertSelective(user);
    }

    @RequestMapping("/update")
    @ResponseBody
    public Object update(User user){
        return userService.update(user);
    }

    @RequestMapping("/listAll")
    @ResponseBody
    public Object listByMap(){
        return  userService.listAll();
    }
}
